

#import "RDVTabBarController.h"
#import "RDVTabBarItem.h"
#import <objc/runtime.h>
#import <RongIMKit/RongIMKit.h>
#import "HttpRequest.h"
#import "TheLoginViewController.h"

#import "CashierNewViewController.h"
//#define tabarHeight [UIScreen mainScreen].bounds.size.height <659? (49):(49)
@interface UIViewController (RDVTabBarControllerItemInternal)

- (void)rdv_setTabBarController:(RDVTabBarController *)tabBarController;

@end

@interface RDVTabBarController ()<RCIMUserInfoDataSource,UIGestureRecognizerDelegate, RCIMConnectionStatusDelegate,
RCIMReceiveMessageDelegate>
{
    UIView *_contentView;
    NSString *userName;
    UIView*canView;
    NSArray*nameArry;
    NSArray*arrayImage;
    UIView*menuBarView;
}

@property (nonatomic, readwrite) RDVTabBar *tabBar;
@property (nonatomic, readwrite)UIView *blurView;

@end

@implementation RDVTabBarController

#pragma mark - View lifecycle
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self setSelectedIndex:[self selectedIndex]];
    
    [self setTabBarHidden:self.isTabBarHidden animated:NO];
    userName=@"";
    
    NSLog(@"######000000");
    [self.view setBackgroundColor:[UIColor clearColor]];
    //[self.view sendSubviewToBack:view];
    [[self tabBar] setBackgroundColor:[UIColor clearColor]];
    
    [self.view addSubview:[self contentView]];
    self.blurView=[[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-49, ScreenWidth, 49)];
    [self.blurView setBackgroundColor:[UIColor clearColor]];
    UIVisualEffect *blurEffect;
    blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    
    UIVisualEffectView *frostedView;
    frostedView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    
    frostedView.frame = self.blurView.bounds;
    [self.blurView addSubview:frostedView];
    [self.view addSubview:self.blurView];
    //   [self.blurView setHidden:self.tabBarHidden];
    
    [self.view addSubview:[self tabBar]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // NSLog(@"str1111===%@",[Global getRongYunToken]);
    //    if ([[Global getRongYunToken] length]==0) {
    //
    //
    //        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //        NSString* Token = [user objectForKey:@"tokenId"];
    //        if (Token.length>0) {
    //
    //            NSString *str1=[NSString stringWithFormat:@"%d",TARGET_VERSION];
    //            //     NSLog(@"str1111===%@",str1);
    //            NSString *plistPath1 = [[NSBundle mainBundle] pathForResource:str1
    //                                                                   ofType:@"plist"];
    //            NSDictionary *dictionary1 = [[NSDictionary alloc] initWithContentsOfFile:plistPath1];
    //            //    NSLog(@"dictionary1===%@",dictionary1);
    //            NSString *rongYunKey = [dictionary1 objectForKey:@"RongYunKey"];
    ////            NSLog(@"rongYunKey^^^^^===%@",rongYunKey);
    //            NSLog(@"rdvtabbar init rongyun key");
    //            [[RCIM sharedRCIM] initWithAppKey:rongYunKey];//设置容云appKEY
    //
    //            [self RequestRongYunToken];
    //
    //        }
    //
    //    }
    //
    //}
    //-(void)RequestRongYunToken
    //{
    //    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //    NSString* Token = [user objectForKey:@"tokenId"];
    //
    //    NSMutableDictionary *param = [NSMutableDictionary new];
    //    [param setValue:Token forKey:@"tokenId"];
    //    [param setValue:@"user" forKey:@"userType"];
    ////    [param setValue:@"" forKey:@"memberId"];
    //
    //    [HttpRequest requestData:@"Chat/RegChat" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
    ////     NSLog(@"rToken====%@",severDataDict.dataDict);
    //        if (isSuccess) {
    //            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
    //               // NSLog(@"tttttttttt");
    //                [[RCIM sharedRCIM] setConnectionStatusDelegate:self];
    ////                NSInteger status=  [[RCIMClient sharedRCIMClient] getConnectionStatus];
    ////                NSLog(@"status111^^^===%d",status);
    //
    //                NSString *  rongYunToken=[[severDataDict.dataDict objectForKey:@"chatInfo"] objectForKey:@"chatToken"];
    //                [Global setRongYunToken:rongYunToken];
    //
    ////                [Global set:rongYunToken];
    //
    //                [[RCIM sharedRCIM] connectWithToken:rongYunToken success:^(NSString *userId) {
    ////                   NSLog(@"ruserId====%@",userId);
    //                    [[RCIM sharedRCIM] setUserInfoDataSource:self];
    //
    //
    //                } error:^(RCConnectErrorCode status) {
    //                                    NSLog(@"登陆的错误码为:%ld", (long)status);
    //                } tokenIncorrect:^{
    //                    //token过期或者不正确。
    //                    //如果设置了token有效期并且token过期，请重新请求您的服务器获取新的token
    //                    //如果没有设置token有效期却提示token错误，请检查您客户端和服务器的appkey是否匹配，还有检查您获取token的流程。
    //                    NSLog(@"token错误");
    //                }];
    //            }
    //            else{
    //
    //                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
    //                    TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
    //                    [self.navigationController pushViewController:detailViewController animated:YES];
    //                }
    //
    //            }
    //
    //        }
    //        else
    //        {
    //            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //            if ([[user objectForKey:@"userStatus"] isEqualToString:@"E"]) {
    //                [Global showTextHudSub:@"请认证门店！" view:self.view];
    //            }else{
    //                [Global hideProgressHUDInternal:self.view];
    //                [Global showTextHudSub:severDataDict.resultStr view:self.view];
    //            }
    //        }
    //
    //    }];
    //
    //}
    //- (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
    //
    ////    NSLog(@"rdvtabbarstatus###^^^===%d",status);
    //}
    //
    //
    // - (void)getUserInfoWithUserId:(NSString *)userId
    //                   completion:(void(^)(RCUserInfo* userInfo))completion
    //{
    //
    //
    //    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //    NSString* Token = [user objectForKey:@"tokenId"];
    //    NSMutableDictionary *param = [NSMutableDictionary new];
    //    [param setValue:Token forKey:@"tokenId"];
    //    [param setValue:userId forKey:@"userId"];
    ////     NSLog(@"getP=====%@",param);
    //     [HttpRequest requestData:@"Chat/GetChatUserInfo" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
    //    NSLog(@"getR=====%@",severDataDict.dataDict);
    //        if (isSuccess) {
    //            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
    //               NSString * userNameStr=[[severDataDict.dataDict objectForKey:@"chatInfo"] objectForKey:@"chatUserName"];
    //                RCUserInfo *user = [[RCUserInfo alloc]init];
    //                user.userId =userId;
    //                user.name =userNameStr;
    //                user.portraitUri = [[severDataDict.dataDict objectForKey:@"chatInfo"] objectForKey:@"chatUserLogoUrl"];
    //                return completion(user);
    //             }
    //            else{
    //
    //                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
    //
    //
    //                    TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
    //                    [self.navigationController pushViewController:detailViewController animated:YES];
    //                }
    //
    //            }
    //
    //        }
    //        else
    //        {
    //
    //            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //            if ([[user objectForKey:@"userStatus"] isEqualToString:@"E"]) {
    //                [Global showTextHudSub:@"请认证门店！" view:self.view];
    //            }else{
    //                [Global hideProgressHUDInternal:self.view];
    //                [Global showTextHudSub:severDataDict.resultStr view:self.view];
    //            }
    //
    //        }
    //
    //    }];
    
}


//- (NSUInteger)supportedInterfaceOrientations {
//    UIInterfaceOrientationMask orientationMask = UIInterfaceOrientationMaskAll;
//    for (UIViewController *viewController in [self viewControllers]) {
//        if (![viewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
//            return UIInterfaceOrientationMaskPortrait;
//        }
//
//        UIInterfaceOrientationMask supportedOrientations = [viewController supportedInterfaceOrientations];
//
//        if (orientationMask > supportedOrientations) {
//            orientationMask = supportedOrientations;
//        }
//    }
//
//    return orientationMask;
//}
//
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    for (UIViewController *viewCotroller in [self viewControllers]) {
        if (![viewCotroller respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)] ||
            ![viewCotroller shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]) {
            return NO;
        }
    }
    return YES;
}

#pragma mark - Methods

- (UIViewController *)selectedViewController {
    return [[self viewControllers] objectAtIndex:[self selectedIndex]];
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    
    
    
    if (selectedIndex >= self.viewControllers.count) {
        return;
    }
    
    if ([self selectedViewController]) {
        [[self selectedViewController] willMoveToParentViewController:nil];
        [[[self selectedViewController] view] removeFromSuperview];
        [[self selectedViewController] removeFromParentViewController];
    }
    
    _selectedIndex = selectedIndex;
    [[self tabBar] setSelectedItem:[[self tabBar] items][selectedIndex]];
    
    [self setSelectedViewController:[[self viewControllers] objectAtIndex:selectedIndex]];
    [self addChildViewController:[self selectedViewController]];
    [[[self selectedViewController] view] setFrame:[[self contentView] bounds]];
    [[self contentView] addSubview:[[self selectedViewController] view]];
    [[self selectedViewController] didMoveToParentViewController:self];
    
    
}

- (void)setViewControllers:(NSArray *)viewControllers {
    
    if (viewControllers && [viewControllers isKindOfClass:[NSArray class]]) {
        _viewControllers = [viewControllers copy];
        
        NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];
        
        for (UIViewController *viewController in viewControllers) {
            RDVTabBarItem *tabBarItem = [[RDVTabBarItem alloc] init];
            [tabBarItem setTitle:viewController.title];
            [tabBarItems addObject:tabBarItem];
            [viewController rdv_setTabBarController:self];
        }
        
        [[self tabBar] setItems:tabBarItems];
    } else {
        for (UIViewController *viewController in _viewControllers) {
            [viewController rdv_setTabBarController:nil];
        }
        
        _viewControllers = nil;
    }
}

- (NSInteger)indexForViewController:(UIViewController *)viewController {
    UIViewController *searchedController = viewController;
    if ([searchedController navigationController]) {
        searchedController = [searchedController navigationController];
    }
    return [[self viewControllers] indexOfObject:searchedController];
}

- (RDVTabBar *)tabBar {
    if (!_tabBar) {
        _tabBar = [[RDVTabBar alloc] init];
        [_tabBar setBackgroundColor:[UIColor clearColor]];
        [_tabBar setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|
                                      UIViewAutoresizingFlexibleTopMargin|
                                      UIViewAutoresizingFlexibleLeftMargin|
                                      UIViewAutoresizingFlexibleRightMargin|
                                      UIViewAutoresizingFlexibleBottomMargin)];
        [_tabBar setDelegate:self];
    }
    return _tabBar;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        [_contentView setBackgroundColor:[UIColor whiteColor]];
        [_contentView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|
                                           UIViewAutoresizingFlexibleHeight)];
    }
    return _contentView;
}

- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated {
    _tabBarHidden = hidden;
    
    __weak RDVTabBarController *weakSelf = self;
    
    void (^block)() = ^{
        CGSize viewSize = weakSelf.view.bounds.size;
        CGFloat tabBarStartingY = viewSize.height;
        CGFloat contentViewHeight = viewSize.height;
        CGFloat tabBarHeight = CGRectGetHeight([[weakSelf tabBar] frame]);
        
        if (!tabBarHeight) {
            tabBarHeight = tabarHeight;
        }
        
        if (!hidden) {
            tabBarStartingY = viewSize.height - tabBarHeight;
            if (![[weakSelf tabBar] isTranslucent]) {
                contentViewHeight -= ([[weakSelf tabBar] minimumContentHeight] ?: tabBarHeight);
            }
            [[weakSelf tabBar] setHidden:NO];
            [[weakSelf blurView] setHidden:NO];
        }
        [[weakSelf blurView] setFrame:CGRectMake(0, tabBarStartingY, viewSize.width, tabBarHeight)];
        [[weakSelf tabBar] setFrame:CGRectMake(0, tabBarStartingY, viewSize.width, tabBarHeight)];
        [[weakSelf contentView] setFrame:CGRectMake(0, 0, viewSize.width, contentViewHeight)];
    };
    
    void (^completion)(BOOL) = ^(BOOL finished){
        if (hidden) {
            [[weakSelf tabBar] setHidden:YES];
            [[weakSelf blurView] setHidden:YES];
        }
    };
    
    if (animated) {
        [UIView animateWithDuration:0.24 animations:block completion:completion];
    } else {
        block();
        completion(YES);
    }
}

- (void)setTabBarHidden:(BOOL)hidden {
    [self setTabBarHidden:hidden animated:NO];
}

#pragma mark - RDVTabBarDelegate

- (BOOL)tabBar:(RDVTabBar *)tabBar shouldSelectItemAtIndex:(NSInteger)index {
    
    if (index==2) {
        
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self.window makeKeyAndVisible];
        canView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        canView.backgroundColor=[UIColor blackColor];
        //        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        //        canView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        canView.alpha=0.90;
        
        //        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        //        UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        //        blurEffectView.frame = canView.bounds;
        //        [canView insertSubview:blurEffectView atIndex:0];
        
        [self.window addSubview:canView];
        [self loadingMenuBar];
        
        UIButton *buttonimage = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonimage.frame = CGRectMake(ScreenWidth/2-20,   ScreenHeight-65, 40, 40);
        [buttonimage setImage:[UIImage imageNamed:@"guanbi"] forState:UIControlStateNormal];
        [buttonimage addTarget:self action:@selector(closeKeyBoad) forControlEvents:UIControlEventTouchUpInside];
        [self.window addSubview:buttonimage];
        
        
        return NO;
    }else{
        
        if ([[self delegate] respondsToSelector:@selector(tabBarController:shouldSelectViewController:)]) {
            if (![[self delegate] tabBarController:self shouldSelectViewController:[self viewControllers][index]]) {
                return NO;
            }
        }
        
        if ([self selectedViewController] == [self viewControllers][index]) {
            if ([[self selectedViewController] isKindOfClass:[UINavigationController class]]) {
                UINavigationController *selectedController = (UINavigationController *)[self selectedViewController];
                
                if ([selectedController topViewController] != [selectedController viewControllers][0]) {
                    [selectedController popToRootViewControllerAnimated:YES];
                }
            }
            
            return NO;
        }
        
        
        return YES;
    }
}



-(void)loadingMenuBar{
    NSArray*nameBtn=[[NSArray alloc]initWithObjects:@"收银",@"订单",@"会员",@"退货",@"存货",@"入库",@"出库",@"要货",@"核销券",@"调拨",nil];
    NSArray *arrayimage = @[@"shouyin",@"xiaoshoudingdan",@"huiyuan",@"xiaoshoutuihuo_new",@"cunhuoguanli",@"shangpinruku",@"shangpinchuku",@"yaohuoguanli",@"youhuiquan",@"diaobodan"];
    NSArray*colorArray=@[[UIColor colorWithRed:248/255.0 green:110/255.0 blue:76/255.0 alpha:1.0],[UIColor colorWithRed:247/255.0 green:140/255.0 blue:69/255.0 alpha:1.0],[UIColor colorWithRed:245/255.0 green:175/255.0 blue:70/255.0 alpha:1.0],[UIColor colorWithRed:219/255.0 green:91/255.0 blue:151/255.0 alpha:1.0],[UIColor colorWithRed:134/255.0 green:171/255.0 blue:59/255.0 alpha:1.0],[UIColor colorWithRed:87/255.0 green:181/255.0 blue:184/255.0 alpha:1.0],[UIColor colorWithRed:130/255.0 green:113/255.0 blue:192/255.0 alpha:1.0],[UIColor colorWithRed:238/255.0 green:86/255.0 blue:86/255.0 alpha:1.0],[UIColor colorWithRed:103/255.0 green:109/255.0 blue:217/255.0 alpha:1.0],[UIColor colorWithRed:191/255.0 green:180/255.0 blue:56/255.0 alpha:1.0]];
    
    for (int i = 0,j = 0; i < nameBtn.count; i++) {
        if (i % 4 == 0) {
            j++;
        }
     
        UIButton *nameButcenter = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*(i%4), (ScreenHeight-540)+(120*j), ScreenWidth/4, 45)];
        [self.window addSubview:nameButcenter];
       
        UIButton *nameBut = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/4*(i%4),   (ScreenHeight-540)+(120*j)+60, ScreenWidth/4, 15)];
        [nameBut setTitle: [nameBtn objectAtIndex:i]  forState:UIControlStateNormal];
        nameBut.tag=201+i;
        [nameBut setTitleColor:[UIColor whiteColor] forState:0];
        nameBut.titleLabel.font=[UIFont systemFontOfSize:15];
        [nameBut addTarget:self action:@selector(scan:) forControlEvents:UIControlEventTouchUpInside];
        [self.window addSubview:nameBut];
        UIImageView*_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        _imageView.layer.masksToBounds = YES;
        _imageView.layer.cornerRadius = 50/2;
        _imageView.center= nameButcenter.center;
        _imageView.backgroundColor=  colorArray[i];
        [self.window addSubview:_imageView];
        
        UIButton *buttonimage = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonimage.frame = CGRectMake(ScreenWidth/4*(i%4),   (ScreenHeight-540)+(120*j)+10, 45, 45);
        buttonimage.tag = 201+i;
        buttonimage.center=nameButcenter.center;
        [buttonimage setImage:[UIImage imageNamed: [arrayimage objectAtIndex:i]] forState:UIControlStateNormal];
        [buttonimage addTarget:self action:@selector(scan:) forControlEvents:UIControlEventTouchUpInside];
        [self.window addSubview:buttonimage];
    }
    
 
    
}

- (void)scan:(UIButton *)button{
    
    if (button.tag==201) {
        [Global setPlaceOrderPType:@"saleItem"];
        [Global setType:@"SCANCASHIER"];//收银
        [Global setScanTitle:@"扫收银"];
              
    }else if (button.tag==202) {
        [Global setType:@"SCANORDER"];
        [Global setScanTitle:@"扫订单"];
        
        
    }else if (button.tag==203) {
        [Global setType:@"SCANMEMBER"];
        [Global setScanTitle:@"扫会员"];
      
        
        
    }else if (button.tag==204) {
        [Global setPlaceOrderPType:@"saleItem"];
        [Global setType:@"Refund"];
        [Global setScanTitle:@"扫退货"];//@"退货"
        
    }
    
    else if (button.tag==205) {
        [Global setPlaceOrderPType:@"saleItem"];
        [Global setType:@"SCANSTOCKMANAGE"];
        [Global setScanTitle:@"扫存货"];//@"存货"
        
    } else if (button.tag==206) {
        [Global setPlaceOrderPType:@"saleItem"];
        [Global setType:@"WhsIn"];//入库
        [Global setScanTitle:@"扫入库"];
       
        
    }else if (button.tag==207) {
        
        [Global setPlaceOrderPType:@"saleItem"];
        [Global setType:@"WhsOut"];
        [Global setScanTitle:@"扫出库"]; //出库
        
    }else if (button.tag==208) {
        [Global setPlaceOrderPType:@"saleItem"];
        [Global setType:@"NeedOrder"];
        [Global setScanTitle:@"扫要货"];//要货

//        [Global setPlaceOrderPType:@"saleItem"];
//        [Global setType:@"CheckVouch"];
//        [Global setScanTitle:@"扫盘点"];//盘点
    }else if (button.tag==209) {
        [Global setType:@"SCANCONPON"];
        [Global setScanTitle:@"核销券"];//要货
    }else if (button.tag==210) {
        [Global setPlaceOrderPType:@"saleItem"];
        [Global setType:@"Allot"];
        [Global setScanTitle:@"扫调拨"];//要货

        
    }else if (button.tag==211) {
        
        
    }
    
    [self setSelectedIndex:2];
    [self.window makeKeyWindow];
    self.window.hidden=YES;

}

-(void)closeKeyBoad
{
    [self.window makeKeyWindow];
    self.window.hidden=YES;
    
    
}
- (void)tabBar:(RDVTabBar *)tabBar didSelectItemAtIndex:(NSInteger)index {
    if (index < 0 || index >= [[self viewControllers] count]) {
        return;
    }
    [self setSelectedIndex:index];
    
    if ([[self delegate] respondsToSelector:@selector(tabBarController:didSelectViewController:)]) {
        [[self delegate] tabBarController:self didSelectViewController:[self viewControllers][index]];
    }
}

@end

#pragma mark - UIViewController+RDVTabBarControllerItem

@implementation UIViewController (RDVTabBarControllerItemInternal)

- (void)rdv_setTabBarController:(RDVTabBarController *)tabBarController {
    
    
    objc_setAssociatedObject(self, @selector(rdv_tabBarController), tabBarController, OBJC_ASSOCIATION_ASSIGN);
}

@end

@implementation UIViewController (RDVTabBarControllerItem)

- (RDVTabBarController *)rdv_tabBarController {
    RDVTabBarController *tabBarController = objc_getAssociatedObject(self, @selector(rdv_tabBarController));
    
    if (!tabBarController && self.parentViewController) {
        tabBarController = [self.parentViewController rdv_tabBarController];
    }
    
    return tabBarController;
}

- (RDVTabBarItem *)rdv_tabBarItem {
    RDVTabBarController *tabBarController = [self rdv_tabBarController];
    NSInteger index = [tabBarController indexForViewController:self];
    return [[[tabBarController tabBar] items] objectAtIndex:index];
}

- (void)rdv_setTabBarItem:(RDVTabBarItem *)tabBarItem {
    RDVTabBarController *tabBarController = [self rdv_tabBarController];
    if (!tabBarController) {
        return;
    }
    
    RDVTabBar *tabBar = [tabBarController tabBar];
    NSInteger index = [tabBarController indexForViewController:self];
    NSMutableArray *tabBarItems = [[NSMutableArray alloc] initWithArray:[tabBar items]];
    [tabBarItems replaceObjectAtIndex:index withObject:tabBarItem];
    [tabBar setItems:tabBarItems];
}

@end

